//
//  MBUtils.m
//  Oldies Hits
//
//  Created by Diep Duong on 12/6/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "MBUtils.h"
#import "CJSONDeserializer.h"
#import "Cryptor.h"
#import <CommonCrypto/CommonHMAC.h>

@implementation MBUtils

+ (NSString *)URLEncode:(NSString *)value 
{
    if (!value)
		return @"";
    value = [value stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    value = [value stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
    value = [value stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
    value = [value stringByReplacingOccurrencesOfString:@"/" withString:@"%2F"];    
    value = [value stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];    
    value = [value stringByReplacingOccurrencesOfString:@"\"" withString:@"%22"];
    value = [value stringByReplacingOccurrencesOfString:@"[" withString:@"%5B"];
    value = [value stringByReplacingOccurrencesOfString:@"]" withString:@"%5D"];
    value = [value stringByReplacingOccurrencesOfString:@"," withString:@"%2C"];
    
    return value;
}

+ (NSString *)URLDecode:(NSString *)value 
{
    if (!value)
		return @"";
    value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    value = [value stringByReplacingOccurrencesOfString:@"%2B" withString:@"+"];
    value = [value stringByReplacingOccurrencesOfString:@"%26" withString:@"&"];
    value = [value stringByReplacingOccurrencesOfString:@"%2F" withString:@"/"];    
    value = [value stringByReplacingOccurrencesOfString:@"%3D" withString:@"="];    
    value = [value stringByReplacingOccurrencesOfString:@"%22" withString:@"\""];
    return value;
}

+ (NSString *)encrypt:(NSString *)text {
    if (text == nil || [text length] == 0) {
        return @"";
    }
    //	DLog(@"Encrypt(in): %@", text);
	Cryptor *des = [[Cryptor alloc] initWithKey:@"abc123"];
	NSString *ret = [des encrypt:text];
    //	DLog(@"Encrypt(out): %@", ret);
	[des release];
	return ret;
}


+ (NSString *)decrypt:(NSString *)text {
    if (text == nil || [text length] == 0) {
        return @"";
    }
    //    DLog(@"Decrypt(in): %@", text);
	Cryptor *des = [[Cryptor alloc] initWithKey:@"abc123"];
	NSString *ret = [des decrypt:text];
    //    DLog(@"Decrypt(out): %@", ret);
	[des release];
	return ret;
}

+ (NSString *)getMD5String:(NSString *)input
{
    const char *cStr = [input UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
    return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],result[8], result[9], result[10], result[11],result[12], result[13], result[14], result[15]]; 	

}

+ (NSString *)getCachesDir {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cacheDirectory = [paths objectAtIndex:0];
    return cacheDirectory;
}

@end
